/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_layer_sample.h
 * Desc:    Mesh layer sample shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		texture2DAlphaFadeDiscard(s_texture_10,s_texcoord_9.x);
	#endif
	
	half mask = texture(s_texture_0,s_texcoord_0.xy * mask_transform.xy + mask_transform.zw).x;
	half layer = saturate((mask - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	float2 texcoord_0 = s_texcoord_0.xy * layer_0_transform.xy + layer_0_transform.zw;
	float2 texcoord_1 = s_texcoord_0.xy * layer_1_transform.xy + layer_1_transform.zw;
	
	half4 diffuse_0 = texture(s_texture_1,texcoord_0) * diffuse_0_color;
	half4 diffuse_1 = texture(s_texture_4,texcoord_1) * diffuse_1_color;
	half4 diffuse = lerp(diffuse_0,diffuse_1,layer);
	
	#ifdef ALPHA_TEST
		#ifdef MULTISAMPLE_0 || USE_ARB_SAMPLE_SHADING
			if(diffuse.w <= 0.5f) discard;
		#else
			diffuse.w = diffuse.w * 4.0f - 1.5f;
			if(diffuse.w <= 0.2f) discard;
		#endif
	#endif
	
	half3 normal_0 = texture2DNormal(s_texture_2,texcoord_0);
	half3 normal_1 = texture2DNormal(s_texture_5,texcoord_1);
	half3 normal = normalize(lerp(normal_0,normal_1,layer));
	
	half4 specular_0 = texture(s_texture_3,texcoord_0) * specular_0_color;
	half4 specular_1 = texture(s_texture_6,texcoord_1) * specular_1_color;
	half4 specular = lerp(specular_0,specular_1,layer);
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		texture2DAlphaFadeDiscard(s_texture_10,s_sampler_10,IN.position.xy,IN.texcoord_9.x);
	#endif
	
	half mask = s_texture_0.Sample(s_sampler_0,IN.texcoord_0.xy * mask_transform.xy + mask_transform.zw).x;
	half layer = saturate((mask - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	float2 texcoord_0 = IN.texcoord_0.xy * layer_0_transform.xy + layer_0_transform.zw;
	float2 texcoord_1 = IN.texcoord_0.xy * layer_1_transform.xy + layer_1_transform.zw;
	
	half4 diffuse_0 = s_texture_1.Sample(s_sampler_1,texcoord_0) * diffuse_0_color;
	half4 diffuse_1 = s_texture_4.Sample(s_sampler_4,texcoord_1) * diffuse_1_color;
	half4 diffuse = lerp(diffuse_0,diffuse_1,layer);
	
	#ifdef ALPHA_TEST
		#ifdef MULTISAMPLE_0 || USE_ALPHA_TEST_LEVEL_10_1
			if(diffuse.w <= 0.5f) discard;
		#else
			diffuse.w = diffuse.w * 4.0f - 1.5f;
			if(diffuse.w <= 0.2f) discard;
		#endif
	#endif
	
	half3 normal_0 = texture2DNormal(s_texture_2,s_sampler_2,texcoord_0);
	half3 normal_1 = texture2DNormal(s_texture_5,s_sampler_5,texcoord_1);
	half3 normal = normalize(lerp(normal_0,normal_1,layer));
	
	half4 specular_0 = s_texture_3.Sample(s_sampler_3,texcoord_0) * specular_0_color;
	half4 specular_1 = s_texture_6.Sample(s_sampler_6,texcoord_1) * specular_1_color;
	half4 specular = lerp(specular_0,specular_1,layer);
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		tex2DAlphaFadeDiscard(s_texture_10,IN.texcoord_7,IN.texcoord_7.z);
	#endif
	
	half mask = tex2D(s_texture_0,IN.texcoord_0.xy * mask_transform.xy + mask_transform.zw).x;
	half layer = saturate((mask - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	float2 texcoord_0 = IN.texcoord_0.xy * layer_0_transform.xy + layer_0_transform.zw;
	float2 texcoord_1 = IN.texcoord_0.xy * layer_1_transform.xy + layer_1_transform.zw;
	
	half4 diffuse_0 = tex2D(s_texture_1,texcoord_0) * diffuse_0_color;
	half4 diffuse_1 = tex2D(s_texture_4,texcoord_1) * diffuse_1_color;
	half4 diffuse = lerp(diffuse_0,diffuse_1,layer);
	
	#ifdef ALPHA_TEST
		#ifdef MULTISAMPLE_0 || !USE_ALPHA_TO_COVERAGE
			if(diffuse.w <= 0.5f) discard;
		#else
			diffuse.w = diffuse.w * 4.0f - 1.5f;
		#endif
	#endif
	
	half3 normal_0 = tex2DNormal(s_texture_2,texcoord_0);
	half3 normal_1 = tex2DNormal(s_texture_5,texcoord_1);
	half3 normal = normalize(lerp(normal_0,normal_1,layer));
	
	half4 specular_0 = tex2D(s_texture_3,texcoord_0) * specular_0_color;
	half4 specular_1 = tex2D(s_texture_6,texcoord_1) * specular_1_color;
	half4 specular = lerp(specular_0,specular_1,layer);
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		tex2DAlphaFadeDiscard(s_texture_10,IN.texcoord_9,IN.texcoord_9.z);
	#endif
	
	half mask = h1tex2D(s_texture_0,IN.texcoord_0.xy * mask_transform.xy + mask_transform.zw);
	half layer = saturate((mask - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	float2 texcoord_0 = IN.texcoord_0.xy * layer_0_transform.xy + layer_0_transform.zw;
	float2 texcoord_1 = IN.texcoord_0.xy * layer_1_transform.xy + layer_1_transform.zw;
	
	half4 diffuse_0 = h4tex2D(s_texture_1,texcoord_0) * diffuse_0_color;
	half4 diffuse_1 = h4tex2D(s_texture_4,texcoord_1) * diffuse_1_color;
	half4 diffuse = lerp(diffuse_0,diffuse_1,layer);
	
	#ifdef ALPHA_TEST
		if(diffuse.w <= 0.5f) discard;
	#endif
	
	half3 normal_0 = tex2DNormal(s_texture_2,texcoord_0);
	half3 normal_1 = tex2DNormal(s_texture_5,texcoord_1);
	half3 normal = normalize(lerp(normal_0,normal_1,layer));
	
	half4 specular_0 = h4tex2D(s_texture_3,texcoord_0) * specular_0_color;
	half4 specular_1 = h4tex2D(s_texture_6,texcoord_1) * specular_1_color;
	half4 specular = lerp(specular_0,specular_1,layer);
	
#endif
